<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-12-5
 * Time: 下午9:26
 */
namespace app\wateradmin\controller;

use app\common\controller\BaseAdmin;
use think\Db;

class Adminsupplier extends BaseAdmin{
    /*
     * 后台首页
     * 赵坤
     * 20161206
     * */
    public function index(){
        $this->assign('title','拼图云供货商管理');
        $this->assign('description','拼图云商城');
        $condition = 'seller_id = '.session('shop');
        $query = array();
        if(isset($_GET['key'])&&!empty($_GET['key'])){
            $condition .= ' AND  supplier_name like "%'.$this->request->param('key').'%" OR supplier_tel like "%'.$this->request->param('key').'%"';
            $this->assign('key',trim($_GET['key']));
            $query = array('key'=>$_GET['key']);
        }
        if(isset($_GET['start_time'])&&!empty($_GET['start_time'])&&isset($_GET['end_time'])&&!empty($_GET['end_time'])){
            $start_time=strtotime($_GET['start_time']);
            $end_time=strtotime($_GET['end_time']);
            $condition .= ' AND (addtime >='.$start_time.' AND addtime <='.$end_time.')';
            $this->assign('start_time',$start_time);
            $this->assign('end_time',$end_time);
            $query = array(
                'start_time'=>$_GET['start_time'],
                'end_time'=>$_GET['end_time']
            );
        }
        $supplier_count = Db::name('shop_supplier')->where($condition)->count();
        if(!empty($supplier_count)){
            $supplier_list = Db::name('shop_supplier')->where($condition)->paginate(15,false,['query'=>$query]);
            $list = $supplier_list->getIterator();
            foreach ($list as $key=>$value){
                $list[$key]['order_count'] = Db::name('shop_order')->where(array('supplier_id'=>$value['supplier_id']))->count();
                $list[$key]['order_sum'] = Db::name('shop_order')->where(array('supplier_id'=>$value['supplier_id'],'order_state'=>array('>',10)))->sum('order_amount');
            }
            $this->assign('supplier_count',$supplier_count);
            $this->assign('supplier_list',$list);
            $this->assign('page',$supplier_list->render());
        }else{
            $this->assign('supplier_count',0);
            $this->assign('supplier_list',array());
        }
        return $this->fetch();
    }
    /*
     * 供货商添加
     * 赵坤
     * 20161206
     * */
    public function add(){
        $this->assign('title','拼图云供货商添加');
        $this->assign('description','拼图云商城');
        if($this->request->isPost()){
            $supplier_count = Db::name('shop_supplier')->where(array('supplier_tel'=>$this->request->param('supplier_mobile'),'seller_id'=>session('shop')))->count();
            if($supplier_count>0){
                return json(array('status'=>0,'info'=>'此供货商已存在'));
            }
            $supplier = array(
                'seller_id'=>session('shop'),
                'supplier_avatar'=>$this->request->param('headimg'),
                'supplier_name'=>$this->request->param('supplier_name'),
                'supplier_tel'=>$this->request->param('supplier_mobile'),
                'supplier_pass'=>md5($this->request->param('supplier_pass')),
                'is_build'=>$this->request->param('is_build'),
                'old_login_time'=>time(),
                'login_time'=>time(),
                'addtime'=>time(),
                'login_ip'=>getIp(),
                'old_login_ip'=>getIp(),
            );
            $id = Db::name('shop_supplier')->insertGetId($supplier);
            if($id!==false){
                return json(array('status'=>1));
            }else{
                return json(array('status'=>0,'info'=>'添加失败'));
            }
        }
        return $this->fetch();
    }
    /*
     * 供货商日志
     * 赵坤
     * 20161206
     * */
    public function log(){
        $this->assign('title','拼图云供货商添加');
        $this->assign('description','拼图云商城');

        return $this->fetch();
    }
}